package com.zjeav.lingjiao.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.maning.mndialoglibrary.MProgressDialog;

import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.BaseActivity;
import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.RegisterRequest;
import com.zjeav.lingjiao.base.response.ValidateCodeResponse;
import com.zjeav.lingjiao.base.tools.SharedPreferencesUtils;
import com.zjeav.lingjiao.base.utils.UIUtils;
import com.zjeav.lingjiao.ui.login.LoginActivty;

public class RegisterDetialActivity extends SwipeBackActivity implements View.OnClickListener, RegisterView {

    EditText pw_ed;
    EditText pw_a_ed;
    Button register_bn;
    RegisterPresenter presenter;
    String phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_detail_layout);
        pw_ed = findViewById(R.id.pw_ed);
        pw_a_ed = findViewById(R.id.pw_a_ed);
        register_bn = findViewById(R.id.next_bn);
        register_bn.setOnClickListener(this);
        presenter = new RegisterPresenter(this);
        phone = getIntent().getStringExtra("phone");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next_bn:
               if ("".equals(pw_ed.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if ("".equals(pw_a_ed.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (!pw_a_ed.getText().toString().equals(pw_ed.getText().toString())) {
                    Toast.makeText(this, "确认密码和你输入的密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    MProgressDialog.showProgress(this, "注册中...");
                    RegisterRequest request = new RegisterRequest(phone, pw_ed.getText().toString(), pw_a_ed.getText().toString());
                    presenter.register(request);
                }
                break;
        }
    }

    @Override
    public void showCodeSuccess(ValidateCodeResponse response) {

    }

    @Override
    public void showCodeFail() {
        if (MProgressDialog.isShowing()) {
            MProgressDialog.dismissProgress();
        }
    }

    @Override
    public void registerSuccess(Result result) {
        if (MProgressDialog.isShowing()) {
            MProgressDialog.dismissProgress();
        }
        if(result.getCode()==1){
            UIUtils.showToast(result.getMessage());
        }else {

            SharedPreferencesUtils.setParam(this, "username", phone);
            SharedPreferencesUtils.setParam(this, "password", pw_ed.getText().toString());

            Toast.makeText(this,"注册成功!",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, LoginActivty.class);
            startActivity(intent);
        }

    }
}
